/**
 * Copyright (c) 2019-2029, wwww.kiwipeach.cn (liuburu@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * https://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cn.kiwipeach.blog.controller;


import cn.kiwipeach.blog.base.AjaxResponse;
import cn.kiwipeach.blog.domain.ArchiveBlogTimelineVO;
import cn.kiwipeach.blog.domain.BlogInfoDetailVO;
import cn.kiwipeach.blog.generator.domain.Blog;
import cn.kiwipeach.blog.service.IBlogCommService;
import cn.kiwipeach.blog.service.IBlogService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 博客 前端控制器
 *
 * @author kiwipeach
 * @since 1.0
 */
@Api(tags = "博客模块")
@Controller
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private IBlogCommService iBlogCommService;

    @Autowired
    @Qualifier("abstractBlogServiceImpl")
    private IBlogService iBlogService;

    @ApiOperation(value = "获取博客分页信息", notes = "用于通过标签作为查询条件过滤博客信息。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "所属分类ID", required = false, paramType = "query"),
            @ApiImplicitParam(name = "tagId", value = "所属标签ID", required = false, paramType = "query")
    })
    @GetMapping("query")
    @ResponseBody
    public AjaxResponse<IPage<Blog>> pageQuery(Page page,
                                               @RequestParam(required = false, value = "categoryId") String categoryId,
                                               @RequestParam(required = false, value = "tagId") String tagId) {
        return iBlogService.pageQuery(page, categoryId, tagId);
    }

    @ApiOperation(value = "获取单条博客详情信息", notes = "用于通过点击博客条目查询博客详情信息。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "blogId", value = "博客ID", required = true, paramType = "path"),
    })
    @GetMapping("detail/{blogId}")
    @ResponseBody
    public BlogInfoDetailVO queryBlogDetail(@PathVariable("blogId") String blogId) {
        return iBlogService.queryById(blogId);
    }


    @ApiOperation(value = "分页查询博客归档信息", notes = "通过制定博客的归档格式查找博客的简略信息。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pattern", value = "博客归档查询类型", required = false, paramType = "query", defaultValue = "yyyy"),
    })
    @GetMapping(value = "archive/query")
    @ResponseBody
    public AjaxResponse<IPage<ArchiveBlogTimelineVO>> archiveBlogQuery(Page page,
                                                                       @RequestParam(value = "pattern", defaultValue = "yyyy") String pattern) {
        return iBlogService.archiveBlogQuery(page, pattern);
    }


    @ApiOperation(value = "博客点赞", notes = "给博客点赞，通常在一定时间内，一个session只能点一次。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "blogId", value = "博客ID", required = true, paramType = "path"),
    })
    @PostMapping(value = "agree/{blogId}")
    @ResponseBody
    public AjaxResponse<Boolean> makeBlogAgree(@PathVariable("blogId") String blogId, HttpServletRequest request) {
        return iBlogService.createBlogAgree(blogId, request.getSession().getId());
    }


}
